package controllers;

import models.Event;
import models.Profile;
import models.Turn;
import play.cache.Cache;
import play.data.binding.Binder;
import play.db.Model;
import play.exceptions.TemplateNotFoundException;
import play.mvc.With;

import java.lang.reflect.Constructor;

/**
 * Created by IntelliJ IDEA.
 * User: bicho
 * Date: 16/01/12
 * Time: 20:58
 * To change this template use File | Settings | File Templates.
 */

@With(Secure.class)
@Check(Profile.SUPERADMIN)
@CRUD.For(Event.class)
public class Events extends CRUD {

    public static void create() throws Exception {
        ObjectType type = ObjectType.get(getControllerClass());
        notFoundIfNull(type);
        Constructor<?> constructor = type.entityClass.getDeclaredConstructor();
        constructor.setAccessible(true);
        Event object = (Event) constructor.newInstance();
        Binder.bindBean(params.getRootParamNode(), "object", object);
        validation.valid(object);
        if (validation.hasErrors()) {
            renderArgs.put("error", play.i18n.Messages.get("crud.hasErrors"));
            try {
                render(request.controller.replace(".", "/") + "/blank.html", type, object);
            } catch (TemplateNotFoundException e) {
                render("CRUD/blank.html", type, object);
            }
        }

        object.indexContent  = object.description;
        object._save();
        createTurnsForEvent((Event) object);

        flash.success(play.i18n.Messages.get("crud.created", type.modelName));
        if (params.get("_save") != null) {
            redirect(request.controller + ".list");
        }
        if (params.get("_saveAndAddAnother") != null) {
            redirect(request.controller + ".blank");
        }
        redirect(request.controller + ".show", object._key());
    }


    public static void saveCustom(String id, String nick) throws Exception {
//        Event eventByNick = Event.getByNick(nick);

        ObjectType type = ObjectType.get(getControllerClass());
        notFoundIfNull(type);
        Model object = type.findById(id);
        notFoundIfNull(object);
        Binder.bindBean(params.getRootParamNode(), "object", object);
        validation.valid(object);
        if (validation.hasErrors()) {
            renderArgs.put("error", play.i18n.Messages.get("crud.hasErrors"));
            try {
                render(request.controller.replace(".", "/") + "/show.html", type, object);
            } catch (TemplateNotFoundException e) {
                render("CRUD/show.html", type, object);
            }
        }
        Event event = (Event) object;
        event.lastNick = nick;
        event.save();

        Cache.delete(nick + "event");

        flash.success(play.i18n.Messages.get("crud.saved", type.modelName));
        if (params.get("_save") != null) {
            redirect(request.controller + ".list");
        }
        redirect(request.controller + ".show", object._key());
    }

    private static void createTurnsForEvent(Event event) {
        Turn turnMorning = new Turn();
        turnMorning.event = event;
        turnMorning.name = "mañana";
        turnMorning.position = 1;
        turnMorning.save();

        Turn turnAfternon = new Turn();
        turnAfternon.event = event;
        turnAfternon.name = "tarde";
        turnMorning.position = 2;
        turnAfternon.save();

        Turn turnEvening = new Turn();
        turnEvening.event = event;
        turnEvening.name = "noche";
        turnMorning.position = 3;
        turnEvening.save();
    }
}
